#include "NewtonInterpolation.hpp"

using namespace std;


// 计算切比雪夫节点
vector<double> chebyshevNodes(int n) {
    vector<double> nodes(n);
    for (int i = 0; i < n; ++i) {
        nodes[i] = cos((2 * i + 1) * M_PI / (2 * n));
    }
    return nodes;
}

// 定义目标函数
double targetFunction(double x) {
    return 1.0 / (1.0 + 25 * x * x);
}

// 生成插值数据并保存到文件
void generateInterpolationData(const string& filename, const vector<double>& nodes) {
    vector<double> y;
    for (double x : nodes) {
        y.push_back(targetFunction(x));
    }

    NewtonInterpolation interpolator(nodes, y);
    ofstream outFile(filename);
    if (!outFile) {
        cerr << "Error opening file for writing." << endl;
        return;
    }

    double step = 0.01; // 步长
    for (double x = -1.0; x <= 1.0; x += step) {
        double interpolated = interpolator(x);
        double actual = targetFunction(x);
        outFile << x << " " << interpolated << " " << actual << endl;
    }

    outFile.close();
    cout << "Data written to " << filename << endl;
}

int main() {
    for (int n : {5, 10, 15, 20}) {
        vector<double> nodes = chebyshevNodes(n);
        string filename = "./data/dataC_n" + to_string(n) + ".txt";
        generateInterpolationData(filename, nodes);
    }
    return 0;
}